<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Feb 4, 2013
 * Time: 4:05:34 PM
 * To change this template use File | Settings | File Templates.
 */

class PrivilegeCode {
    private $db;
    private $id;
    private $privilegeName;
    private $privilegeDes;
    private $status;
    private $isDelete;
    private $version;

    public function __construct() {
        $ado = new AdodbDriver();
        $this->db = $ado->pConnect(true);
        $this->db->debug = false;
    }

    function __set($name, $value) {
        // TODO: Implement __set() method.
        $this->$name = $value;

    }

    function __get($name) {
        // TODO: Implement __get() method.
        return $this->$name;
    }

    public function save() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            $privilege = new PrivilegeAR();
            if ($this->id != '' AND $this->id != NULL) {
                $privilege->Load("id=$this->id");
            } else {
                $privilege->create_time = date('Y-m-d H:i:s', time());
                $privilege->create_user_id = $_SESSION['user_id'];
                $privilege->is_delete = 0;
            }
            $privilege->code = $this->privilegeName;
            $privilege->description = $this->privilegeDes;
            $privilege->status = $this->status;
            $privilege->update_user_id = $_SESSION['user_id'];
            $privilege->version = $this->version;
            $i = $privilege->save();
        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

    public function getAllPrivilege() {
        $sql = 'select id, code from aea_privilege Where is_delete=0';
        $allPrivilege = $this->db->Execute($sql);
        return $allPrivilege->GetAssoc();
    }

    public function getPrivilege($roleId, $code = true) {
        if ($code) {
            $sql = "select aea_privilege.id, code from aea_privilege Where aea_privilege.is_delete=0
                AND aea_privilege.id IN (SELECT distinct privilege_id from aea_role_privilege where aea_role_privilege.role_id ='$roleId')";
        } else {
            $sql = "SELECT distinct id,privilege_id from aea_role_privilege where aea_role_privilege.role_id ='$roleId' order by privilege_id asc";
        }

        $allPrivilege = $this->db->Execute($sql);
        return $allPrivilege->GetAssoc();
    }

    public function getNotPrivilege($roleId) {
        $sql = "select aea_privilege.id, code from aea_privilege Where aea_privilege.is_delete=0
                AND aea_privilege.id NOT IN (SELECT distinct privilege_id from aea_role_privilege where aea_role_privilege.role_id ='$roleId')";
        $allPrivilege = $this->db->Execute($sql);
        return $allPrivilege->GetAssoc();
    }

    public function assignRoleToPrivilege($add_values, $delete_values, $role_id) {
        if (!empty($delete_values)) {
            $delArray = array_values($delete_values);
            $sql = "delete from aea_role_privilege where role_id = $role_id AND  privilege_id IN (" . implode(', ', $delArray) . ")";
            $resultDelete = $this->db->Execute($sql);
            $delStatus = true;
        } else
            $delStatus = false;
        if (!empty($add_values)) {
            $addArray = array_values($add_values);
            $uid = $_SESSION['user_id'];
            $create_time = date('Y-m-d H:i:s', time());
            $sql = "INSERT INTO aea_role_privilege(role_id,create_user_id,update_user_id,create_time,privilege_id) values($role_id,$uid,$uid,'$create_time',?)";
            for ($i = 0; $i < count($addArray); $i++) {
                $resultAdd = $this->db->Execute($sql, $addArray[$i]);
            }
            $addStatus = true;
        } else
            $addStatus = false;
        $results = array('add' => $resultAdd, 'del' => $resultDelete, 'detStatus' => $delStatus, 'addStatus' => $addStatus);
        return $results;
        // return $allPrivilege->GetAssoc();
    }

    public function getGrid($posStart, $count, $name, $status, $sort_by, $sort_dir) {
        $sql = "SELECT id,code,description,status FROM aea_privilege Where is_delete=0";
        if ($name != '')
            $sql .= " and code like '$name%'";
        if ($status != '')
            $sql .= " and status = '$status'";

        if ($sort_dir == '')
            $sort_dir = "asc";
        if ($sort_by != '')
            $sql .= " Order By $sort_by $sort_dir";
        if ($posStart == 0) {
            $sqlCount = "Select count(*) as cnt from ($sql) as tbl";

            $resCount = $this->db->Execute($sqlCount);
            if (!$resCount)
                print $this->db->ErrorMsg();
            else
                while (!$resCount->EOF) {
                    $totalCount = $resCount->fields[0];
                    $resCount->MoveNext();
                }

        }
        $sql .= " LIMIT " . $posStart . "," . $count;
        $res = $this->db->Execute($sql);
        if ($res) {
            $xmlString = "<?xml version=\"1.0\"  encoding=\"ISO-8859-1\"?><rows total_count='" . $totalCount . "' pos='" . $posStart . "'>";
            while (!$res->EOF) {
                $xmlString .= "<row id='" . $res->fields['id'] . "'>
                    <cell>" . $res->fields['id'] . "</cell>
                    <cell>" . $res->fields['code'] . "</cell>
                    <cell>" . $res->fields['description'] . "</cell>
                    <cell>" . ($res->fields['status'] == 1 ? "Active" : "Incative") . "</cell>
                    <cell>" . $res->fields['status'] . "</cell>
                    </row>";
                $posStart++;
                $res->MoveNext();
            }
            $xmlString .= "</rows>";
        } else {
            $xmlString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $xmlString;
    }
}

require_once('libraries/adodb5/adodb-active-record.inc.php');
class PrivilegeAR extends ADOdb_Active_Record {
    var $_table = 'aea_privilege';

}